package zzps.exeed.web.route;

import zzps.exeed.web.http.HttpMethod;

import java.util.HashMap;
import java.util.Map;

public class RouteTable {

    Map<String,Route> routeTableMap = new HashMap<>(1024 * 128);

    public void add(Route route){
        synchronized (this){
            String path = route.getPath();
            Route r = routeTableMap.get(path);
            if (r != null){
                throw new RuntimeException("路由path重复:" + path);
            }
            routeTableMap.put(path,route);
        }
    }

    public Route match(String path, HttpMethod httpMethod){
        Route route = routeTableMap.get(path);
        if (route == null){
            return null;
        }
        if (route.match(httpMethod)){
            return route;
        }
        return null;
    }
}
